home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK2.toast / Development Kits / Hardware / Mac OS USB DDK v1.3f3 / Examples / USBKeypad / USBKeypadKeyIn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-20  |  22.0 KB  |  918 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        USBKeypadKeyIn.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12.  
  13. /*
  14.  USB Keyboard Translation to Macintosh 
  15.  */
  16.  
  17. #include <Types.h>
  18. #include <Events.h>
  19. #include <Resources.h>
  20. #include <LowMem.h>
  21. #include <USB.h>
  22. #include "USBKeypad.h"
  23.  
  24. extern    usbKeyPadPBStruct myParamBlock;
  25. extern    usbKeyPadPBStruct shimParamBlock;
  26.  
  27.  
  28. #define DOWN        0
  29. #define UP            1
  30.  
  31. #define TRUE        1
  32. #define FALSE        0
  33.  
  34. #define FakeADBAddr        16
  35. #define FakeKBDType        2        // this should be the same as the Apple extended keyboard for now
  36.  
  37. static UInt32    myKeyMAP[4];
  38. static UInt32    keyTransState;
  39. static Handle    handleKCHR;
  40. static UInt8*    KCHRptr;
  41.  
  42. typedef KeyMap * KeyMapPtr;
  43.  
  44. /* prototypes */
  45. Boolean KeyInArray(UInt8 key, UInt8 *array, UInt16 len);
  46. Boolean SetBit(UInt8 *bitmapArray, UInt16 index, Boolean value); 
  47. void  PostADBKeyToMac(UInt16 virtualKeycode, UInt8 state);
  48.  
  49. /* when we move to master interfaces we can get this stuff from LowMemPriv.h */
  50. /* be sure to turn on DIRECT_LOWMEM_ACCESSORS */
  51. #define LMSetKbdVars(value) ((*(short *)0x0216) = (value))
  52. #define LMSetKeyLast(value) ((*(short *)0x0184) = (value))
  53. #define LMSetKeyTime(value) ((*(long *)0x0186) = (value))
  54. #define LMSetKeyRepTime(value) ((*(long *)0x018A) = (value))
  55. #define LMSetKeyMap(KeyMapValue)    BlockMove((Ptr)(KeyMapValue), (Ptr)0x0174, sizeof(KeyMap))
  56.  
  57. // index represents USB keyboard usage value, content is Mac virtual keycode
  58. static UInt8    USBKMAPNumLockDown[256] = {  
  59.     0xFF,     /* 00 no event */        
  60.     0xFF,    /* 01 ErrorRollOver */    
  61.     0xFF,    /* 02 POSTFail */    
  62.     0xFF,    /* 03 ErrorUndefined */    
  63.     0x00,    /* 04 A */
  64.     0x0B,    /* 05 B */
  65.     0x08,    /* 06 C */
  66.     0x02,    /* 07 D */
  67.     0x0E,    /* 08 E */
  68.     0x03,    /* 09 F */
  69.     0x05,    /* 0A G */
  70.     0x04,    /* 0B H */
  71.     0x22,    /* 0C I */
  72.     0x26,    /* 0D J */
  73.     0x28,    /* 0E K */
  74.     0x25,    /* 0F L */
  75.  
  76.     0x2E,     /* 10 M */        
  77.     0x2D,    /* 11 N */    
  78.     0x1F,    /* 12 O */    
  79.     0x23,    /* 13 P */    
  80.     0x0C,    /* 14 Q */
  81.     0x0F,    /* 15 R */
  82.     0x01,    /* 16 S */
  83.     0x11,    /* 17 T */
  84.     0x20,    /* 18 U */
  85.     0x09,    /* 19 V */
  86.     0x0D,    /* 1A W */
  87.     0x07,    /* 1B X */
  88.     0x10,    /* 1C Y */
  89.     0x06,    /* 1D Z */
  90.     0x12,    /* 1E 1/! */
  91.     0x13,    /* 1F 2/@ */
  92.  
  93.     0x14,     /* 20 3 # */        
  94.     0x15,    /* 21 4 $ */    
  95.     0x17,    /* 22 5 % */    
  96.     0x16,    /* 23 6 ^ */    
  97.     0x1A,    /* 24 7 & */
  98.     0x1C,    /* 25 8 * */
  99.     0x19,    /* 26 9 ( */
  100.     0x1D,    /* 27 0 ) */
  101.     0x24,    /* 28 Return (Enter) */
  102.     0x35,    /* 29 ESC */
  103.     0x33,    /* 2A Delete (Backspace) */
  104.     0x30,    /* 2B Tab */
  105.     0x31,    /* 2C Spacebar */
  106.     0x1B,    /* 2D - _ */
  107.     0x18,    /* 2E = + */
  108.     0x21,    /* 2F [ { */
  109.  
  110.     0x1E,     /* 30 ] } */        
  111.     0x2A,    /* 31 \ | */    
  112.     0xFF,    /* 32 Non-US # and ~ (what?!!!) */    
  113.     0x29,    /* 33 ; : */    
  114.     0x27,    /* 34 ' " */
  115.     0x32,    /* 35 ` ~ */
  116.     0x2B,    /* 36 , < */
  117.     0x2F,    /* 37 . > */
  118.     0x2C,    /* 38 / ? */
  119.     0x39,    /* 39 Caps Lock */
  120.     0x7A,    /* 3A F1 */
  121.     0x78,    /* 3B F2 */
  122.     0x63,    /* 3C F3 */
  123.     0x76,    /* 3D F4 */
  124.     0x60,    /* 3E F5 */
  125.     0x61,    /* 3F F6 */
  126.  
  127.     0x62,     /* 40 F7 */        
  128.     0x64,    /* 41 F8 */    
  129.     0x65,    /* 42 F9 */    
  130.     0x6D,    /* 43 F10 */    
  131.     0x67,    /* 44 F11 */
  132.     0x6F,    /* 45 F12 */
  133.     0x69,    /* 46 F13/PrintScreen */
  134.     0x6B,    /* 47 F14/ScrollLock */
  135.     0x71,    /* 48 F15/Pause */                
  136.     0x72,    /* 49 Insert */
  137.     0x73,    /* 4A Home */
  138.     0x74,    /* 4B PageUp */
  139.     0x75,    /* 4C Delete Forward */
  140.     0x77,    /* 4D End */
  141.     0x79,    /* 4E PageDown */
  142.     0x7C,    /* 4F RightArrow */
  143.  
  144.     0x7B,     /* 50 LeftArrow */        
  145.     0x7D,    /* 51 DownArrow */    
  146.     0x7E,    /* 52 UpArrow */    
  147.     0x47,    /* 53 NumLock/Clear */    
  148.     0x4B,    /* 54 Keypad / */
  149.     0x43,    /* 55 Keypad * */
  150.     0x4E,    /* 56 Keypad - */
  151.     0x45,    /* 57 Keypad + */
  152.     0x4C,    /* 58 Keypad Enter */
  153.     0x53,    /* 59 Keypad 1 */
  154.     0x54,    /* 5A Keypad 2 */
  155.     0x55,    /* 5B Keypad 3 */
  156.     0x56,    /* 5C Keypad 4 */
  157.     0x57,    /* 5D Keypad 5 */
  158.     0x58,    /* 5E Keypad 6 */
  159.     0x59,    /* 5F Keypad 7 */
  160.  
  161.     0x5B,     /* 60 Keypad 8 */        
  162.     0x5C,    /* 61 Keypad 9 */    
  163.     0x52,    /* 62 Keypad 0 */    
  164.     0x41,    /* 63 Keypad . */    
  165.     0xFF,    /* 64 Non-US \ and  | (what ??!!) */
  166.     0x6E,    /* 65 ApplicationKey (not on a mac!)*/
  167.     0x7F,    /* 66 PowerKey  */
  168.     0x51,    /* 67 Keypad = */
  169.     0x69,    /* 68 F13 */
  170.     0x6B,    /* 69 F14 */
  171.     0x71,    /* 6A F15 */
  172.     0xFF,    /* 6B F16 */
  173.     0xFF,    /* 6C F17 */
  174.     0xFF,    /* 6D F18 */
  175.     0xFF,    /* 6E F19 */
  176.     0xFF,    /* 6F F20 */
  177.  
  178.     0x5B,     /* 70 F21 */        
  179.     0x5C,    /* 71 F22 */    
  180.     0x52,    /* 72 F23 */    
  181.     0x41,    /* 73 F24 */    
  182.     0xFF,    /* 74 Execute */
  183.     0xFF,    /* 75 Help */
  184.     0x7F,    /* 76 Menu */
  185.     0x4C,    /* 77 Select */
  186.     0x69,    /* 78 Stop */
  187.     0x6B,    /* 79 Again */
  188.     0x71,    /* 7A Undo */
  189.     0xFF,    /* 7B Cut */
  190.     0xFF,    /* 7C Copy */
  191.     0xFF,    /* 7D Paste */
  192.     0xFF,    /* 7E Find */
  193.     0xFF,    /* 7F Mute */
  194.     
  195.     0xFF,     /* 80 no event */        
  196.     0xFF,    /* 81 no event */    
  197.     0xFF,    /* 82 no event */    
  198.     0xFF,    /* 83 no event */    
  199.     0xFF,    /* 84 no event */
  200.     0xFF,    /* 85 no event */
  201.     0xFF,    /* 86 no event */
  202.     0xFF,    /* 87 no event */
  203.     0xFF,    /* 88 no event */
  204.     0xFF,    /* 89 no event */
  205.     0xFF,    /* 8A no event */
  206.     0xFF,    /* 8B no event */
  207.     0xFF,    /* 8C no event */
  208.     0xFF,    /* 8D no event */
  209.     0xFF,    /* 8E no event */
  210.     0xFF,    /* 8F no event */
  211.  
  212.     0xFF,     /* 90 no event */        
  213.     0xFF,    /* 91 no event */    
  214.     0xFF,    /* 92 no event */    
  215.     0xFF,    /* 93 no event */    
  216.     0xFF,    /* 94 no event */
  217.     0xFF,    /* 95 no event */
  218.     0xFF,    /* 96 no event */
  219.     0xFF,    /* 97 no event */
  220.     0xFF,    /* 98 no event */
  221.     0xFF,    /* 99 no event */
  222.     0xFF,    /* 9A no event */
  223.     0xFF,    /* 9B no event */
  224.     0xFF,    /* 9C no event */
  225.     0xFF,    /* 9D no event */
  226.     0xFF,    /* 9E no event */
  227.     0xFF,    /* 9F no event */
  228.  
  229.     0xFF,     /* A0 no event */        
  230.     0xFF,    /* A1 no event */    
  231.     0xFF,    /* A2 no event */    
  232.     0xFF,    /* A3 no event */    
  233.     0xFF,    /* A4 no event */
  234.     0xFF,    /* A5 no event */
  235.     0xFF,    /* A6 no event */
  236.     0xFF,    /* A7 no event */
  237.     0xFF,    /* A8 no event */
  238.     0xFF,    /* A9 no event */
  239.     0xFF,    /* AA no event */
  240.     0xFF,    /* AB no event */
  241.     0xFF,    /* AC no event */
  242.     0xFF,    /* AD no event */
  243.     0xFF,    /* AE no event */
  244.     0xFF,    /* AF no event */
  245.  
  246.     0xFF,     /* B0 no event */        
  247.     0xFF,    /* B1 no event */    
  248.     0xFF,    /* B2 no event */    
  249.     0xFF,    /* B3 no event */    
  250.     0xFF,    /* B4 no event */
  251.     0xFF,    /* B5 no event */
  252.     0xFF,    /* B6 no event */
  253.     0xFF,    /* B7 no event */
  254.     0xFF,    /* B8 no event */
  255.     0xFF,    /* B9 no event */
  256.     0xFF,    /* BA no event */
  257.     0xFF,    /* BB no event */
  258.     0xFF,    /* BC no event */
  259.     0xFF,    /* BD no event */
  260.     0xFF,    /* BE no event */
  261.     0xFF,    /* BF no event */
  262.  
  263.     0xFF,     /* C0 no event */        
  264.     0xFF,    /* C1 no event */    
  265.     0xFF,    /* C2 no event */    
  266.     0xFF,    /* C3 no event */    
  267.     0xFF,    /* C4 no event */
  268.     0xFF,    /* C5 no event */
  269.     0xFF,    /* C6 no event */
  270.     0xFF,    /* C7 no event */
  271.     0xFF,    /* C8 no event */
  272.     0xFF,    /* C9 no event */
  273.     0xFF,    /* CA no event */
  274.     0xFF,    /* CB no event */
  275.     0xFF,    /* CC no event */
  276.     0xFF,    /* CD no event */
  277.     0xFF,    /* CE no event */
  278.     0xFF,    /* CF no event */
  279.  
  280.     0xFF,     /* D0 no event */        
  281.     0xFF,    /* D1 no event */    
  282.     0xFF,    /* D2 no event */    
  283.     0xFF,    /* D3 no event */    
  284.     0xFF,    /* D4 no event */
  285.     0xFF,    /* D5 no event */
  286.     0xFF,    /* D6 no event */
  287.     0xFF,    /* D7 no event */
  288.     0xFF,    /* D8 no event */
  289.     0xFF,    /* D9 no event */
  290.     0xFF,    /* DA no event */
  291.     0xFF,    /* DB no event */
  292.     0xFF,    /* DC no event */
  293.     0xFF,    /* DD no event */
  294.     0xFF,    /* DE no event */
  295.     0xFF,    /* DF no event */
  296.  
  297.     0x3B,     /* E0 left control key */        
  298.     0x38,    /* E1 left shift key key */    
  299.     0x3A,    /* E2 left alt/option key */    
  300.     0x37,    /* E3 left GUI (windows/cmd) key */    
  301.     
  302.     0x3E,    /* E4 right control key */
  303.     0x3C,    /* E5 right shift key key */
  304.     0x3D,    /* E6 right alt/option key */
  305.     0x37,    /* E7 right GUI (windows/cmd) key */
  306.     0xFF,    /* E8 no event */
  307.     0xFF,    /* E9 no event */
  308.     0xFF,    /* EA no event */
  309.     0xFF,    /* EB no event */
  310.     0xFF,    /* EC no event */
  311.     0xFF,    /* ED no event */
  312.     0xFF,    /* EE no event */
  313.     0xFF,    /* EF no event */
  314.     
  315.     0xFF,     /* F0 no event */        
  316.     0xFF,    /* F1 no event */    
  317.     0xFF,    /* F2 no event */    
  318.     0xFF,    /* F3 no event */    
  319.     0xFF,    /* F4 no event */
  320.     0xFF,    /* F5 no event */
  321.     0xFF,    /* F6 no event */
  322.     0xFF,    /* F7 no event */
  323.     0xFF,    /* F8 no event */
  324.     0xFF,    /* F9 no event */
  325.     0xFF,    /* FA no event */
  326.     0xFF,    /* FB no event */
  327.     0xFF,    /* FC no event */
  328.     0xFF,    /* FD no event */
  329.     0xFF,    /* FE no event */
  330.     0xFF,    /* FF no event */
  331. };
  332.         
  333. // index represents USB keyboard usage value, content is Mac virtual keycode
  334. static UInt8    USBKMAPNumLockUp[256] = {  
  335.     0xFF,     /* 00 no event */        
  336.     0xFF,    /* 01 ErrorRollOver */    
  337.     0xFF,    /* 02 POSTFail */    
  338.     0xFF,    /* 03 ErrorUndefined */    
  339.     0x00,    /* 04 A */
  340.     0x0B,    /* 05 B */
  341.     0x08,    /* 06 C */
  342.     0x02,    /* 07 D */
  343.     0x0E,    /* 08 E */
  344.     0x03,    /* 09 F */
  345.     0x05,    /* 0A G */
  346.     0x04,    /* 0B H */
  347.     0x22,    /* 0C I */
  348.     0x26,    /* 0D J */
  349.     0x28,    /* 0E K */
  350.     0x25,    /* 0F L */
  351.  
  352.     0x2E,     /* 10 M */        
  353.     0x2D,    /* 11 N */    
  354.     0x1F,    /* 12 O */    
  355.     0x23,    /* 13 P */    
  356.     0x0C,    /* 14 Q */
  357.     0x0F,    /* 15 R */
  358.     0x01,    /* 16 S */
  359.     0x11,    /* 17 T */
  360.     0x20,    /* 18 U */
  361.     0x09,    /* 19 V */
  362.     0x0D,    /* 1A W */
  363.     0x07,    /* 1B X */
  364.     0x10,    /* 1C Y */
  365.     0x06,    /* 1D Z */
  366.     0x12,    /* 1E 1/! */
  367.     0x13,    /* 1F 2/@ */
  368.  
  369.     0x14,     /* 20 3 # */        
  370.     0x15,    /* 21 4 $ */    
  371.     0x17,    /* 22 5 % */    
  372.     0x16,    /* 23 6 ^ */    
  373.     0x1A,    /* 24 7 & */
  374.     0x1C,    /* 25 8 * */
  375.     0x19,    /* 26 9 ( */
  376.     0x1D,    /* 27 0 ) */
  377.     0x24,    /* 28 Return (Enter) */
  378.     0x35,    /* 29 ESC */
  379.     0x33,    /* 2A Delete (Backspace) */
  380.     0x30,    /* 2B Tab */
  381.     0x31,    /* 2C Spacebar */
  382.     0x1B,    /* 2D - _ */
  383.     0x18,    /* 2E = + */
  384.     0x21,    /* 2F [ { */
  385.  
  386.     0x1E,     /* 30 ] } */        
  387.     0x2A,    /* 31 \ | */    
  388.     0xFF,    /* 32 Non-US # and ~ (what?!!!) */    
  389.     0x29,    /* 33 ; : */    
  390.     0x27,    /* 34 ' " */
  391.     0x32,    /* 35 ` ~ */
  392.     0x2B,    /* 36 , < */
  393.     0x2F,    /* 37 . > */
  394.     0x2C,    /* 38 / ? */
  395.     0x39,    /* 39 Caps Lock */
  396.     0x7A,    /* 3A F1 */
  397.     0x78,    /* 3B F2 */
  398.     0x63,    /* 3C F3 */
  399.     0x76,    /* 3D F4 */
  400.     0x60,    /* 3E F5 */
  401.     0x61,    /* 3F F6 */
  402.  
  403.     0x62,     /* 40 F7 */        
  404.     0x64,    /* 41 F8 */    
  405.     0x65,    /* 42 F9 */    
  406.     0x6D,    /* 43 F10 */    
  407.     0x67,    /* 44 F11 */
  408.     0x6F,    /* 45 F12 */
  409.     0x69,    /* 46 F13/PrintScreen */
  410.     0x6B,    /* 47 F14/ScrollLock */
  411.     0x71,    /* 48 F15/Pause */                
  412.     0x72,    /* 49 Insert */
  413.     0x73,    /* 4A Home */
  414.     0x74,    /* 4B PageUp */
  415.     0x75,    /* 4C Delete Forward */
  416.     0x77,    /* 4D End */
  417.     0x79,    /* 4E PageDown */
  418.     0x7C,    /* 4F RightArrow */
  419.  
  420.     0x7B,     /* 50 LeftArrow */        
  421.     0x7D,    /* 51 DownArrow */    
  422.     0x7E,    /* 52 UpArrow */    
  423.     0x47,    /* 53 NumLock/Clear */    
  424.     0x4B,    /* 54 Keypad / */
  425.     0x43,    /* 55 Keypad * */
  426.     0x4E,    /* 56 Keypad - */
  427.     0x45,    /* 57 Keypad + */
  428.     0x4C,    /* 58 Keypad Enter */
  429.     0x77,    /* 59 Keypad 1 = End on keyapd*/
  430.     0x7D,    /* 5A Keypad 2 = Down Arrow on keypad*/
  431.     0x79,    /* 5B Keypad 3 = Page Down on keypad*/
  432.     0x7B,    /* 5C Keypad 4 = Left Arrow on keypad*/
  433.     0x57,    /* 5D Keypad 5 */
  434.     0x7C,    /* 5E Keypad 6 = Right Arrow on keypad*/
  435.     0x73,    /* 5F Keypad 7 = HOME on keypad*/
  436.  
  437.     0x7E,     /* 60 Keypad 8 = Up Arrow on keypad*/        
  438.     0x74,    /* 61 Keypad 9 = Page Up on keypad*/    
  439.     0x72,    /* 62 Keypad 0 = Insert on keypad*/    
  440.     0x75,    /* 63 Keypad . = Delete (forward) on keypad*/    
  441.     0xFF,    /* 64 Non-US \ and  | (what ??!!) */
  442.     0x6E,    /* 65 ApplicationKey (not on a mac!)*/
  443.     0x7F,    /* 66 PowerKey  */
  444.     0x51,    /* 67 Keypad = */
  445.     0x69,    /* 68 F13 */
  446.     0x6B,    /* 69 F14 */
  447.     0x71,    /* 6A F15 */
  448.     0xFF,    /* 6B F16 */
  449.     0xFF,    /* 6C F17 */
  450.     0xFF,    /* 6D F18 */
  451.     0xFF,    /* 6E F19 */
  452.     0xFF,    /* 6F F20 */
  453.  
  454.     0x5B,     /* 70 F21 */        
  455.     0x5C,    /* 71 F22 */    
  456.     0x52,    /* 72 F23 */    
  457.     0x41,    /* 73 F24 */    
  458.     0xFF,    /* 74 Execute */
  459.     0xFF,    /* 75 Help */
  460.     0x7F,    /* 76 Menu */
  461.     0x4C,    /* 77 Select */
  462.     0x69,    /* 78 Stop */
  463.     0x6B,    /* 79 Again */
  464.     0x71,    /* 7A Undo */
  465.     0xFF,    /* 7B Cut */
  466.     0xFF,    /* 7C Copy */
  467.     0xFF,    /* 7D Paste */
  468.     0xFF,    /* 7E Find */
  469.     0xFF,    /* 7F Mute */
  470.     
  471.     0xFF,     /* 80 no event */        
  472.     0xFF,    /* 81 no event */    
  473.     0xFF,    /* 82 no event */    
  474.     0xFF,    /* 83 no event */    
  475.     0xFF,    /* 84 no event */
  476.     0xFF,    /* 85 no event */
  477.     0xFF,    /* 86 no event */
  478.     0xFF,    /* 87 no event */
  479.     0xFF,    /* 88 no event */
  480.     0xFF,    /* 89 no event */
  481.     0xFF,    /* 8A no event */
  482.     0xFF,    /* 8B no event */
  483.     0xFF,    /* 8C no event */
  484.     0xFF,    /* 8D no event */
  485.     0xFF,    /* 8E no event */
  486.     0xFF,    /* 8F no event */
  487.  
  488.     0xFF,     /* 90 no event */        
  489.     0xFF,    /* 91 no event */    
  490.     0xFF,    /* 92 no event */    
  491.     0xFF,    /* 93 no event */    
  492.     0xFF,    /* 94 no event */
  493.     0xFF,    /* 95 no event */
  494.     0xFF,    /* 96 no event */
  495.     0xFF,    /* 97 no event */
  496.     0xFF,    /* 98 no event */
  497.     0xFF,    /* 99 no event */
  498.     0xFF,    /* 9A no event */
  499.     0xFF,    /* 9B no event */
  500.     0xFF,    /* 9C no event */
  501.     0xFF,    /* 9D no event */
  502.     0xFF,    /* 9E no event */
  503.     0xFF,    /* 9F no event */
  504.  
  505.     0xFF,     /* A0 no event */        
  506.     0xFF,    /* A1 no event */    
  507.     0xFF,    /* A2 no event */    
  508.     0xFF,    /* A3 no event */    
  509.     0xFF,    /* A4 no event */
  510.     0xFF,    /* A5 no event */
  511.     0xFF,    /* A6 no event */
  512.     0xFF,    /* A7 no event */
  513.     0xFF,    /* A8 no event */
  514.     0xFF,    /* A9 no event */
  515.     0xFF,    /* AA no event */
  516.     0xFF,    /* AB no event */
  517.     0xFF,    /* AC no event */
  518.     0xFF,    /* AD no event */
  519.     0xFF,    /* AE no event */
  520.     0xFF,    /* AF no event */
  521.  
  522.     0xFF,     /* B0 no event */        
  523.     0xFF,    /* B1 no event */    
  524.     0xFF,    /* B2 no event */    
  525.     0xFF,    /* B3 no event */    
  526.     0xFF,    /* B4 no event */
  527.     0xFF,    /* B5 no event */
  528.     0xFF,    /* B6 no event */
  529.     0xFF,    /* B7 no event */
  530.     0xFF,    /* B8 no event */
  531.     0xFF,    /* B9 no event */
  532.     0xFF,    /* BA no event */
  533.     0xFF,    /* BB no event */
  534.     0xFF,    /* BC no event */
  535.     0xFF,    /* BD no event */
  536.     0xFF,    /* BE no event */
  537.     0xFF,    /* BF no event */
  538.  
  539.     0xFF,     /* C0 no event */        
  540.     0xFF,    /* C1 no event */    
  541.     0xFF,    /* C2 no event */    
  542.     0xFF,    /* C3 no event */    
  543.     0xFF,    /* C4 no event */
  544.     0xFF,    /* C5 no event */
  545.     0xFF,    /* C6 no event */
  546.     0xFF,    /* C7 no event */
  547.     0xFF,    /* C8 no event */
  548.     0xFF,    /* C9 no event */
  549.     0xFF,    /* CA no event */
  550.     0xFF,    /* CB no event */
  551.     0xFF,    /* CC no event */
  552.     0xFF,    /* CD no event */
  553.     0xFF,    /* CE no event */
  554.     0xFF,    /* CF no event */
  555.  
  556.     0xFF,     /* D0 no event */        
  557.     0xFF,    /* D1 no event */    
  558.     0xFF,    /* D2 no event */    
  559.     0xFF,    /* D3 no event */    
  560.     0xFF,    /* D4 no event */
  561.     0xFF,    /* D5 no event */
  562.     0xFF,    /* D6 no event */
  563.     0xFF,    /* D7 no event */
  564.     0xFF,    /* D8 no event */
  565.     0xFF,    /* D9 no event */
  566.     0xFF,    /* DA no event */
  567.     0xFF,    /* DB no event */
  568.     0xFF,    /* DC no event */
  569.     0xFF,    /* DD no event */
  570.     0xFF,    /* DE no event */
  571.     0xFF,    /* DF no event */
  572.  
  573.     0x3B,     /* E0 left control key */        
  574.     0x38,    /* E1 left shift key key */    
  575.     0x3A,    /* E2 left alt/option key */    
  576.     0x37,    /* E3 left GUI (windows/cmd) key */    
  577.     
  578.     0x3E,    /* E4 right control key */
  579.     0x3C,    /* E5 right shift key key */
  580.     0x3D,    /* E6 right alt/option key */
  581.     0x37,    /* E7 right GUI (windows/cmd) key */
  582.     0xFF,    /* E8 no event */
  583.     0xFF,    /* E9 no event */
  584.     0xFF,    /* EA no event */
  585.     0xFF,    /* EB no event */
  586.     0xFF,    /* EC no event */
  587.     0xFF,    /* ED no event */
  588.     0xFF,    /* EE no event */
  589.     0xFF,    /* EF no event */
  590.     
  591.     0xFF,     /* F0 no event */        
  592.     0xFF,    /* F1 no event */    
  593.     0xFF,    /* F2 no event */    
  594.     0xFF,    /* F3 no event */    
  595.     0xFF,    /* F4 no event */
  596.     0xFF,    /* F5 no event */
  597.     0xFF,    /* F6 no event */
  598.     0xFF,    /* F7 no event */
  599.     0xFF,    /* F8 no event */
  600.     0xFF,    /* F9 no event */
  601.     0xFF,    /* FA no event */
  602.     0xFF,    /* FB no event */
  603.     0xFF,    /* FC no event */
  604.     0xFF,    /* FD no event */
  605.     0xFF,    /* FE no event */
  606.     0xFF,    /* FF no event */
  607. };
  608.         
  609. void
  610. InitUSBKeyboard()
  611. {
  612.     handleKCHR = GetResource('KCHR',0);    // US keyboard mapping (handled differently by ADB Mgr)
  613.     HLock(handleKCHR);
  614.     KCHRptr = (UInt8 *)*handleKCHR;
  615. }
  616.  
  617.  
  618.  
  619. void PostUSBKeyToMac(UInt16 rawUSBkey)
  620. {
  621. static    UInt8    oldLEDState = 0x00;
  622. static    UInt8    newLEDState = 0x00;
  623.  
  624. static    UInt8    capsLockState = 0x00;
  625. static    UInt8    numLockState = 0x00;
  626. static    UInt8    scrollLockState = 0x00;
  627.  
  628. register UInt8    virtualKeycode, keystate;
  629.  
  630.     
  631.     if (KCHRptr == 0)
  632.     {
  633.         InitUSBKeyboard();
  634.     }
  635.     
  636.     keystate = (rawUSBkey & 0x8000) ? UP : DOWN;
  637.     rawUSBkey &= 0x0FF;
  638.     
  639.     if (keystate == DOWN)
  640.     {
  641.         newLEDState = oldLEDState;
  642.         
  643.         switch (rawUSBkey)
  644.         {
  645. // Note:  This switch statement is being left it to make it easy to add "toggling" keys in the future.
  646. //        it used to support toggled numlock & scroll lock...  It doesn't anymore.
  647.             case kUSBNumLockKey:
  648.                 newLEDState ^= (1 << kNumLockLED);
  649.                 keystate = (newLEDState & (1 << kNumLockLED)) ? DOWN : UP;
  650.                 break;
  651.         }
  652.         
  653.         if (newLEDState != oldLEDState)
  654.         {
  655.             oldLEDState = newLEDState;
  656.             //USBHIDControlDevice(kHIDSetLEDStateByBits, &newLEDState);
  657.         }
  658.     }
  659.     
  660.     if (rawUSBkey == kUSBNumLockKey)
  661.     {
  662.         return;
  663.     }
  664.     
  665.     // look up rawUSBkey in KMAP resource to get virtual keycode
  666.     if (rawUSBkey < sizeof(USBKMAPNumLockDown))
  667.     {
  668.         if (oldLEDState & kNumLockLEDMask)
  669.         {
  670.             virtualKeycode = USBKMAPNumLockDown[rawUSBkey];
  671.         }
  672.         else
  673.         {
  674.             virtualKeycode = USBKMAPNumLockUp[rawUSBkey];
  675.         }
  676.     } 
  677.     else 
  678.     {
  679. //        DebugStr("\pPostUSBKeyToMac: Need bigger KMAP table");
  680.         virtualKeycode = 0xFF;
  681.     }
  682.  
  683.     PostADBKeyToMac(virtualKeycode, keystate);
  684.     if (virtualKeycode == 0x7F)
  685.         PostADBKeyToMac(virtualKeycode, keystate);
  686. }
  687.  
  688. void 
  689. PostADBKeyToMac(UInt16 virtualKeycode, UInt8 state)
  690. {
  691.     UInt32    keyEventMsg;
  692.     
  693.     if (virtualKeycode > 127) return;  // not handled by MacOS!
  694.     
  695.     // stop repeating
  696.     LMSetKeyLast(0);
  697.     LMSetKbdVars(0);
  698.     
  699.     // update our keymap
  700.     SetBit((UInt8 *)myKeyMAP, virtualKeycode, state == DOWN ? 1 : 0);
  701.     LMSetKeyMap(&myKeyMAP);
  702.  
  703.     // set this keyboard as the last keyboard
  704.     LMSetKbdLast(FakeADBAddr);
  705.     LMSetKbdType(FakeKBDType);
  706.     
  707.     // call KeyTrans to get character code
  708.     virtualKeycode |= ((myKeyMAP[1]<<9) & 0x00FE00) | ((myKeyMAP[1]>>7) & 0x0100) | ((state==UP) ? 0x080 : 0);
  709.     keyEventMsg = KeyTranslate(KCHRptr, virtualKeycode , &keyTransState);
  710.     virtualKeycode &= 0x7F;
  711.     
  712.     if (keyEventMsg & 0xFFFF0000) {
  713.         // post event
  714.         UInt32 event =(keyEventMsg & 0xFF000000) | ((FakeADBAddr << 16) & 0x0FF0000) | ((virtualKeycode << 8) & 0x0FF00)  | ((keyEventMsg>>16) & 0x0FF);
  715.         if (state == DOWN){
  716.             UInt32 ticks = TickCount();
  717.             LMSetKeyTime(ticks);
  718.             LMSetKeyRepTime(ticks);
  719.             LMSetKeyLast(event & 0x0FFFF);
  720.             LMSetKbdVars((event>>16) & 0x0FFFF);
  721.         }        
  722.         PostEvent((state == DOWN ? keyDown : keyUp), event);
  723.     }
  724.     if (keyEventMsg & 0x0000FFFF) {
  725.         // post event
  726.         UInt32 event =((keyEventMsg<<16) & 0xFF000000) | ((FakeADBAddr << 16) & 0x0FF0000) | ((virtualKeycode << 8) & 0x0FF00)  | (keyEventMsg & 0x0FF);
  727.         if (state == DOWN){
  728.             UInt32 ticks = TickCount();
  729.             LMSetKeyTime(ticks);
  730.             LMSetKeyRepTime(ticks);
  731.             LMSetKeyLast(event & 0x0FFFF);
  732.             LMSetKbdVars((event>>16) & 0x0FFFF);
  733.         }        
  734.         PostEvent((state == DOWN ? keyDown : keyUp), event);
  735.     }
  736. }
  737.  
  738. // Sets the bitmapArray[index] to value
  739. // returns old value;
  740. Boolean
  741. SetBit(UInt8 *bitmapArray, UInt16 index, Boolean value)
  742. {    
  743.     UInt32    mask = 0x1 << (index % 8 );
  744.     Boolean    oldVal;
  745.     
  746.     oldVal = (bitmapArray[index/8] & mask) ? TRUE : FALSE;
  747.     
  748.     if (value){
  749.         bitmapArray[index/8] |= mask;
  750.     }else{
  751.         bitmapArray[index/8] &= ~mask;
  752.     }
  753.     
  754.     return (oldVal);
  755. }
  756.  
  757. void USBDemoKeyIn(UInt32 refcon, void * theData)
  758. {
  759. #pragma unused (refcon)
  760.  
  761. USBHIDDataPtr    pTheKeyboardData;
  762. register        UInt8    i;
  763.  
  764.     pTheKeyboardData = (USBHIDDataPtr)theData;
  765.     for (i=0; i<pTheKeyboardData->kbd.keycount; i++)
  766.     {
  767.         // no shim installed, let's just post some Macintosh keyevents
  768.         PostUSBKeyToMac(pTheKeyboardData->kbd.usbkeycode[i]);
  769.     }
  770.  
  771. }
  772.  
  773. OSStatus USBHIDControlDevice(UInt32 theControlSelector, void * theControlData)
  774. {
  775. #pragma unused (theControlData)
  776.  
  777.     switch (theControlSelector)
  778.     {
  779.         case kHIDSetLEDStateByBits:
  780.             break;
  781.             
  782.         case kHIDEnableDemoMode:
  783.             USBExpertStatus(myParamBlock.deviceRef, "\pTenKey Driver: Demo Mode Enabled", myParamBlock.deviceRef);
  784.             break;
  785.  
  786.         case kHIDDisableDemoMode:
  787.             USBExpertStatus(myParamBlock.deviceRef, "\pTenKey Driver: Demo Mode Disabled", myParamBlock.deviceRef);
  788.             break;
  789.  
  790.         default:
  791.             return paramErr;
  792.     }
  793.     return 0;
  794. }
  795.  
  796. void ProcessInterruptReport(UInt8 hidReport[])
  797. {
  798.  
  799. UInt8    i, j, newkey, oldkey, deltas;
  800.  
  801. UInt8        changedmodifiers, keycount;
  802. USBHIDData    theKeyboardData;
  803. Boolean        keypressedflag, keyreleasedflag;
  804.     
  805.     deltas = 0;
  806.     
  807.     if (myParamBlock.hidEmulationInit == false)
  808.     {
  809.         myParamBlock.hidEmulationInit = true;
  810.         for (i = 0; i < kKeyboardReportSize; i++)
  811.             myParamBlock.oldHIDReport[i] = 0;
  812.     };
  813.     
  814.     myParamBlock.oldHIDReport[1] = 0x0;
  815.     hidReport[1] = 0x0;
  816.     
  817.     for (i = 0; i < kKeyboardReportSize; i++)
  818.     {
  819.         if (hidReport[i] != myParamBlock.oldHIDReport[i])
  820.         {
  821.             deltas++;
  822.         }
  823.     }
  824.     
  825.     if ((myParamBlock.sendRawReportFlag) && deltas)
  826.     {
  827.         USBDemoKeyIn(0xff, (void *)&hidReport[0]);
  828.     }
  829.     else
  830.     {
  831.         if (deltas)
  832.         {
  833.             keycount = 0;
  834.             changedmodifiers = hidReport[0] ^  myParamBlock.oldHIDReport[0];
  835.             
  836.             if (changedmodifiers)
  837.             {
  838.                 for (i = 0; i < kKeyboardModifierBits; i++)
  839.                 {
  840.                     if (changedmodifiers & (1 << i))
  841.                     {
  842.                         if (hidReport[0] & (1 << i))
  843.                         {
  844.                             theKeyboardData.kbd.usbkeycode[keycount++] = (0xe0 + i);
  845.                         }
  846.                         else
  847.                         {
  848.                             theKeyboardData.kbd.usbkeycode[keycount++] = (0x80e0 + i);
  849.                         }
  850.                     }
  851.                 }
  852.                 myParamBlock.oldHIDReport[0] = hidReport[0];
  853.             }
  854.             
  855.             if ((hidReport[kKeyboardOffsetToKeys] == 0) || (hidReport[kKeyboardOffsetToKeys] > 0x03))
  856.             {
  857.                 // While this double loop may look strange, I refer you to appendix C, of the HID Devices specification.
  858.                 // pp. 73 & 74 clearly state that report order is abitrary and does not reflect the order of events.
  859.                 // to quote: "The order of keycodes in array fields has no significance.  Order determination is done
  860.                 // by the host software comparing the contents of the previous report to the current report.  If two or 
  861.                 // more keys are pressed in one report, their order is indeterminate.  Keyboards may buffer events that
  862.                 // would have otherwise resulted in multiple events in a single report".
  863.                 
  864.                 // Because this specification (or lack thereof) states that the order is indeterminant, we have to 
  865.                 // check all the old keys against all the new keys...
  866.                 
  867.                 for (i = kKeyboardOffsetToKeys; i < (kKeyboardOffsetToKeys + kKeyboardReportKeys); i++)
  868.                 {
  869.                     keypressedflag = true;
  870.                     newkey = hidReport[i];
  871.                     
  872.                     keyreleasedflag = true;
  873.                     oldkey = myParamBlock.oldHIDReport[i];                            
  874.                     for (j = kKeyboardOffsetToKeys; j < (kKeyboardOffsetToKeys + kKeyboardReportKeys); j++)                                // then look through all the keys that were previously and are currently reported as pressed
  875.                     {                                    
  876.                         if (newkey == myParamBlock.oldHIDReport[j])        // was this new key already pressed?
  877.                         {                                
  878.                             keypressedflag = false;                        // if it was, then don't report it as being pressed.
  879.                         }
  880.                         
  881.                         if (oldkey == hidReport[j])                        // Is the old key still pressed?
  882.                         {                                
  883.                             keyreleasedflag = false;                    // If yes, then don't report it as being released
  884.                         }
  885.                     }
  886.                     if ((newkey > 0x03) && keypressedflag)
  887.                     {
  888.                         theKeyboardData.kbd.usbkeycode[keycount++] = newkey;
  889.                     }
  890.                     
  891.                     if ((oldkey > 0x03) && keyreleasedflag)
  892.                     {
  893.                         theKeyboardData.kbd.usbkeycode[keycount++] = (oldkey + 0x8000);
  894.                     }
  895.                     
  896.                     if (keycount > 20)                                    // a worse case scenario is:
  897.                     {                                                    // 4 modifier keys up
  898.                         break;                                            // the other 4 modifier keys down
  899.                     }                                                    // 6 previously reported keys up
  900.                 }                                                        // 6 newly reported keys down
  901.                                                                         // 6 + 6 + 4 + 4 = 20
  902.                 for (i = 0; i<kKeyboardReportSize; i++)
  903.                 {
  904.                     myParamBlock.oldHIDReport[i] = hidReport[i];
  905.                 };
  906.             }
  907.             
  908.             if (keycount)
  909.             {
  910.                 theKeyboardData.kbd.keycount = keycount;
  911.                 USBDemoKeyIn(myParamBlock.interruptRefcon, (void *)&theKeyboardData);
  912.             }
  913.         }
  914.     }
  915. }
  916.  
  917.  
  918.